﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Littlebeer.MDM.MSO
{
    public class EntityManager
    {
        public Hashtable GetUseFullEntityIndentityGroup(List<Littlebeer.Common.FieldMapping.FieldPairModel> mapping)
        {
            Hashtable result = new Hashtable();
            var query = from a in mapping
                        select a.Right.FieldName;
            List<string> Fields = query.Distinct().ToList<string>();

            foreach (Littlebeer.MDM.MSO.EntityModel entity in GetAllEntity()) 
            {
                foreach (Littlebeer.MDM.MSO.EntityIndentityGroup group in entity.EntityIndentityGroups)
                {
                    if (group.Intersect(Fields).Count() == group.Count())
                    {
                        if (!result.ContainsKey(entity.EntityName))
                        {
                            result.Add(entity.EntityName, new List<Littlebeer.MDM.MSO.EntityIndentityGroup>() { group });
                        }
                        else
                        {
                            List<Littlebeer.MDM.MSO.EntityIndentityGroup> current=result[entity.EntityName] as List<Littlebeer.MDM.MSO.EntityIndentityGroup>;
                            current.Add(group);
                            result[entity.EntityName] = current;
                        }
                    }
                }
            }
            return result;
        }
        public List<Littlebeer.MDM.MSO.EntityModel> GetAllEntity()
        {
            List<Littlebeer.MDM.MSO.EntityModel> result = new List<MSO.EntityModel>();
            EntityIndentityGroup group=new EntityIndentityGroup();
            group.Add("CompanyName");
            group.Add("Address");
            EntityIndentityGroup group2 = new EntityIndentityGroup();
            group2.Add("Address");

            result = new List<EntityModel>() {new EntityModel(){ EntityName="Customer", PrimaryKeys="CompanyID", 
                EntityIndentityGroups=new List<EntityIndentityGroup>(){group,group2} } };
            return result;
        }

        public EntityModel Get(string EntityName)
        {
            EntityModel result = new EntityModel();
            var entitys = GetAllEntity().Where(d => d.EntityName == EntityName);
            if (entitys.Count() > 0)
            {
                result = entitys.First<EntityModel>();
            }
            else
            {
                throw (new Exception("can't find the entity!"));
            }
            return result;
        }
        public EntityManager(string conn)
        { 
        
        }
    }
    

}
